package Modelo;
import Conexion.Conexion;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SIS_ROLES{
	private double ID;
	private String DESCRIPCION;
        private Conexion con = null;
        
	public SIS_ROLES(){}

	public SIS_ROLES(double ID,String DESCRIPCION){
		this.ID =ID;
		this.DESCRIPCION =DESCRIPCION;
	}

	public boolean Insertar(){
		try{
			String consulta = "insert into BROKER.SIS_ROLES(ID,DESCRIPCION) VALUES ("+ID+",'"+DESCRIPCION+"')";
			con.EjecutarSentencia(consulta);
                        
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean EliminarXid() throws SQLException{
            String consulta = "delete from BROKER.SIS_ROLES where ID = "+getID();
            con.EjecutarSentencia(consulta);
            return true;
	}

	public SIS_ROLES BuscarXid(double id){
		try{String consulta = "select * from BROKER.SIS_ROLES where ID = "+id;
			ResultSet rs = con.EjecutarConsulta(consulta);
			return Cargar(rs).get(0);}
		catch(Exception e){
			e.printStackTrace();}
			return null;
	}

	public List<SIS_ROLES> Todos(){
		try{String consulta = "select * from BROKER.SIS_ROLES";
			ResultSet rs = con.EjecutarConsulta(consulta);
			return Cargar(rs);}
		catch(Exception e){
			e.printStackTrace();}
			return null;
	}
	public List<SIS_ROLES> buscarXdescripcion(String text){
		try{String consulta = "select * from BROKER.SIS_ROLES where UPPER(SIS_ROLES.DESCRIPCION) LIKE UPPER('%"+text+"%')";
			ResultSet rs = con.EjecutarConsulta(consulta);
			return Cargar(rs);}
		catch(Exception e){
			e.printStackTrace();}
			return null;
	}

        public List<SIS_ROLES> TodosLosInactivos(double perfil){
		try{String consulta = "select * from BROKER.SIS_ROLES sp where sp.ID not in(select ID_ROLES from BROKER.SIS_PERFILES_ROLES where ID_PERFILES = "+perfil+")";
			ResultSet rs = con.EjecutarConsulta(consulta);
			return Cargar(rs);}
		catch(Exception e){
			e.printStackTrace();}
			return null;
	}
        
        public List<SIS_ROLES> buscarTodosLosInactivos(double perfil, String descipcion){
		try{String consulta = "select sp.* from BROKER.SIS_ROLES sp where sp.ID not in(select ID_ROLES from BROKER.SIS_PERFILES_ROLES where ID_PERFILES = "+perfil+") and (UPPER (sp.DESCRIPCION) like  UPPER('%"+descipcion+"%'))";
			ResultSet rs = con.EjecutarConsulta(consulta);
			return Cargar(rs);}
		catch(Exception e){
			e.printStackTrace();}
			return null;
	}
        
	public List<SIS_ROLES> Cargar(ResultSet rs){
		try{
			List<SIS_ROLES> lista = new ArrayList<SIS_ROLES>();
			while(rs.next()){
				SIS_ROLES objeto = new SIS_ROLES();

				objeto.setID(rs.getDouble("ID"));
				objeto.setDESCRIPCION(rs.getString("DESCRIPCION"));
                                objeto.setCon(con);
				lista.add(objeto);
			}
			return lista;
		}
		catch(SQLException ex){
			ex.printStackTrace();
		return null;}
	}

	public double getID(){
		return ID;
	}
	public void setID(double ID){
		this.ID=ID;
	}

	public String getDESCRIPCION(){
		return DESCRIPCION;
	}
	public void setDESCRIPCION(String DESCRIPCION){
		this.DESCRIPCION=DESCRIPCION;
	}

    public Conexion getCon() {
        return con;
    }

    public void setCon(Conexion con) {
        this.con = con;
    }        
}
